package com.xinqi.modules.user.users.third.controller.app;

import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.log.asynclog.annotation.AsyncLog;
import com.xinqi.common.log.asynclog.enums.AsyncOperaType;
import com.xinqi.common.log.asynclog.enums.AsyncType;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.user.third.dto.rsp.UserLoginLogResultDTO;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.user.users.third.service.UserLoginLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * APP -- 设备管理
 *
 * @version V1.0
 * @Title:
 * @ClassName: com.xinqi.modules.user.third.controller.app.UserLoginLogAppController.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/7/17 17:38
 */
@RestController
@RequestMapping("/app/user-login-log")
@RequiredArgsConstructor
public class UserLoginLogAppController {
    private final UserLoginLogService userLoginLogService;

    /**
     * 获取设备详细信息
     *
     * @Title:
     * @MethodName: getInfo
     * [id]
     * @Return com.xinqi.common.base.response.R<com.xinqi.modules.sys.user.user.dto.rsp.UserLoginLogResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/8 10:41
     */
    @GetMapping("/get/{id}")
    public R<UserLoginLogResultDTO> getInfo(@PathVariable Long id) {
        return R.ok(userLoginLogService.findById(id));
    }

    /**
     * 删除设备
     *
     * @Title:
     * @MethodName: remove
     * [id]
     * @Return com.xinqi.common.base.response.R<java.lang.Void>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/8 10:41
     */
    @AsyncLog(title = "删除登录设备", asyncType = AsyncType.USER_INFO, operatorType = AsyncOperaType.USER_LOGIN_LOG)
    @GetMapping("/remove/{id}")
    public R<Void> remove(@PathVariable Long id) {
        if (userLoginLogService.deleteById(id)) {
            return R.ok(MessageUtils.message("sys.user.login.log.remove.success"));
        }
        return R.fail(MessageUtils.message("sys.user.login.log.remove.fail"));
    }

    /**
     * 查询用户设备列表
     *
     * @Title:
     * @MethodName: list
     * []
     * @Return com.xinqi.common.base.response.R<java.util.List < com.xinqi.modules.sys.user.user.dto.rsp.UserLoginLogResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/8 10:41
     */
    @PostMapping("list")
    public R<List<UserLoginLogResultDTO>> list() {
        Long userId = LoginHelper.getUserId();
        return R.ok(userLoginLogService.findByUserId(userId));
    }

}
